using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Windows.Controls;
using System.Text.RegularExpressions;

namespace ValidationFramework
{
    public class RegexValidationRule: ValidationRuleBase
    {

        #region Private Fields

        private string _pattern;

        #endregion

        /// <summary>
        /// Validates the value if it matches the pattern
        /// </summary>
        /// <param name="value"></param>
        /// <param name="cultureInfo"></param>
        /// <returns></returns>
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);

            if (string.IsNullOrEmpty(_pattern))
            {
                Regex regex = new Regex(_pattern);


                if (!regex.IsMatch(value.ToString()))
                {
                    result = new ValidationResult(false, ErrorMessage);
                }
            }

            return result;
        }

        #region Public Properties

        /// <summary>
        /// Gets or sets the pattern to validate the value for
        /// </summary>
        public string Pattern
        {
            get { return _pattern; }
            set { _pattern = value; }
        }

        #endregion
    }
}
